package com.itheima.tliasmanagementspringboot.controller;


import com.itheima.tliasmanagementspringboot.annotation.OperateLog;
import com.itheima.tliasmanagementspringboot.common.R;
import com.itheima.tliasmanagementspringboot.dtos.PageDto;
import com.itheima.tliasmanagementspringboot.dtos.StudentPageDto;
import com.itheima.tliasmanagementspringboot.pojo.Student;
import com.itheima.tliasmanagementspringboot.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学员管理
 */
@RestController
@Slf4j//日志输出注解,哪里需要加哪里(类上),lombok提供
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * 功能: 学员列表查询
     * @return 统一的返回对象(R)
     */
    @GetMapping("/students/list")
    public R queryAllStudent(){
        log.info("查询学员: {}, {}",1, 2 );//一个{}就是一个占位符,写到文件中去
        return R.ok(studentService.queryAllStudentService());
    }
    /**
     * 功能: 学员列表查询,分页查询
     * @return 统一的返回对象(R)
     */
    @GetMapping("/students")
    public R pageQueryStudent(StudentPageDto studentPageDto){
        log.info("分页查询学员: {}", studentPageDto);//一个{}就是一个占位符,写到文件中去
        System.out.println("请求层:" + studentPageDto);
        return R.ok(studentService.pageQueryStudentService(studentPageDto));
    }
    

    /**
     * 功能: 根据id查询学员信息
     * @return 统一返回对象(R)
     *
     */
    @GetMapping("students/{id}")
    public R queryStudentByCondition(@PathVariable Integer id){
        log.info("根据id查询学员信息: {}", id);
        return R.ok(studentService.queryStudentByConditionService(id));
    }

    /**
     * 功能: 根据id删除学员信息
     * @return 统一返回对象(R)
     *
     */
    @OperateLog
    @DeleteMapping("/students/{ids}")
    public R deleteStudentById(@PathVariable Integer[] ids){
        log.info("根据id删除学员信息: {}", ids);
        studentService.deleteStudentByIds(ids);
        return R.ok();
    }

    /**
     * 功能: 添加/插入一条学员信息
     * @return 统一返回对象(R)
     *
     */
    @OperateLog
    @PostMapping("/students")
    public R insertStudent(@RequestBody Student student){
        log.info("添加/插入一条学员信息:");
        studentService.insertStudentService(student);
        return R.ok();
    }

    /**
     * 功能:根据id更新学员信息
     * @param student :学员id
     * @return 统一返回对象R
     */
    @OperateLog
    @PutMapping("/students")
    public R updateStudentById(@RequestBody Student student){
        log.info("根据id删除学员信息: {}", student);
        studentService.updateStudentByIdService(student);
        return R.ok();
    }

    @OperateLog
    @PutMapping("/students/violation/{id}/{score}")
    public R violationHandler(@PathVariable Integer id, @PathVariable Integer score){
        studentService.violationHandlerService(id, score);
        return R.ok();
    }

}
